package com.cxm.helper.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.cxm.helper.annotation.LogApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

    public static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);


    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    @LogApi(level = LogApi.LogLevel.SECURE)
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            LOGGER.error("10001登陆成功" + System.currentTimeMillis());
            LOGGER.warn("10001登陆成功" + System.currentTimeMillis());
            return "登录成功";
        }
        return "登录失败";
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        System.out.println(StpUtil.getLoginIdAsInt());
        System.out.println(StpUtil.getTokenValue());
        System.out.println(StpUtil.getTokenInfo());
        return "当前会话是否登录：" + StpUtil.isLogin() + StpUtil.getLoginDevice();
    }
}
